package org.PropertyManageMentSystem.Servive;

import org.PropertyManageMentSystem.Data.*;
import org.PropertyManageMentSystem.Manager.*;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

public class WorkerService {

    private Worker worker;

    private MysqlConfig mysqlConfig;

    public WorkerService(Long workerId, MysqlConfig mysqlConfig) throws Exception {
        this.mysqlConfig = mysqlConfig;
        WorkerManager worker_manager = new WorkerManager(this.mysqlConfig);
        this.worker = worker_manager.getWorkerById(workerId);
    }
    public ArrayList<Schedule> getSchedule() throws SQLException, ClassNotFoundException {
        Long workerid = this.worker.getWorkerId();
        ScheduleManager schedule_manager = new ScheduleManager(this.mysqlConfig);
        ArrayList<Schedule> scheduleArrayList = schedule_manager.getScheduleByWorkerId(workerid);
        ArrayList<Schedule> res = new ArrayList<>();
        for(Schedule schedule:scheduleArrayList){
            if(schedule.getState()!= Schedule.ScheduleState.reSchedule &&
                schedule.getState()!= Schedule.ScheduleState.finish)
                res.add(schedule);
        }
        return res;
    }
    private boolean checkBelonging(Long scheduleId) throws SQLException, ClassNotFoundException {
        ArrayList<Schedule> schedules = getSchedule();
        boolean flag = false;
        for (Schedule schedule: schedules)
            if (schedule.getScheduleId()==scheduleId){
                flag = true;
                break;
            }
        return flag;
    }
    public void acceptSchedule(Long scheduleId) throws SQLException, ClassNotFoundException {
        if (checkBelonging(scheduleId)){
            Long workerId = this.worker.getWorkerId();
            ScheduleManager schedule_manager = new ScheduleManager(this.mysqlConfig);
            schedule_manager.acceptSchedule(scheduleId);
            WorkerManager worker_manager = new WorkerManager(this.mysqlConfig);
            worker_manager.changeWorkerStateById(workerId, Worker.WorkerState.busy);
        }
        else System.out.println("schedule id error!");
    }
    public void reSchedule(Long scheduleId,EmergencyType emergencyType) throws SQLException, ClassNotFoundException {
        if (checkBelonging(scheduleId)) {
            Long workerId = this.worker.getWorkerId();
            ScheduleManager schedule_manager = new ScheduleManager(this.mysqlConfig);
            schedule_manager.reSchedule(scheduleId);
            TaskManager taskManager = new TaskManager(mysqlConfig);
            taskManager.changeEmergencyTypeById(schedule_manager.getTaskIdByScheduleId(scheduleId),emergencyType);
            WorkerManager worker_manager = new WorkerManager(this.mysqlConfig);
            worker_manager.changeWorkerStateById(workerId, Worker.WorkerState.free);
        }
        else System.out.println("schedule id error!");
    }

    public void recordAction(Long scheduleId, Timestamp startTime, Timestamp finishTime, String info) throws SQLException, ClassNotFoundException {
        if (checkBelonging(scheduleId)) {
            Long workerId = this.worker.getWorkerId();
            ActionManager action_manager = new ActionManager(this.mysqlConfig);
            action_manager.addAction(scheduleId, workerId, startTime, finishTime, info);
        }
        else System.out.println("schedule id error!");
    }
    public void finishSchedule(Long scheduleId) throws SQLException, ClassNotFoundException {
        if (checkBelonging(scheduleId)) {
            Long workerId = this.worker.getWorkerId();
            ScheduleManager schedule_manager = new ScheduleManager(this.mysqlConfig);
            schedule_manager.finishSchedule(scheduleId);
            WorkerManager worker_manager = new WorkerManager(this.mysqlConfig);
            worker_manager.changeWorkerStateById(workerId, Worker.WorkerState.free);
        }
        else System.out.println("schedule id error!");
    }
    public ArrayList<ComplaintReply> getUnfilledReply(){
        Long workerid = this.worker.getWorkerId();
        ComplaintReplyManager reply_manager = new ComplaintReplyManager(this.mysqlConfig);
        return reply_manager.getUnfilledReply(ComplaintReply.Role.worker, workerid);
    }
    public Complaint getRelatedComplaint(Long complaintReplyId){
        ComplaintManager complaint_manager = new ComplaintManager(this.mysqlConfig);
        return complaint_manager.getComplaintById(complaintReplyId);
    }
    public void fillReply(Long complaintReplyId, String info){
        ComplaintReplyManager reply_manager = new ComplaintReplyManager(this.mysqlConfig);
        reply_manager.fillComplaintReply(complaintReplyId, info);
    }
    public int getTotalWorkTime(int year, int month) throws SQLException, ClassNotFoundException {
        Long workerid = this.worker.getWorkerId();
        ActionManager action_manager = new ActionManager(mysqlConfig);
        long total_time = 0;
        ArrayList<Action> actions = action_manager.getActionByWorkerId(workerid);
        for (Action action : actions) {
            Timestamp starttime = action.getStarttime();
            if (starttime.getYear()+1900 == year && starttime.getMonth()+1 == month)
                total_time += action.getEndtime().getTime()-action.getStarttime().getTime();
        }
        return (int)(total_time/(60*60*1000));
    }
}
